import moment from 'moment';
export const correctDate = (dateStr: string | undefined) => {
    const today = moment();
    dateStr = dateStr?.replace(/[^\d]/g, '').slice(0, 8);
    let year:number;
    let month:number;
    const parsedDate = moment(dateStr, 'YYYYMMDD', true);
    if (dateStr) {
        year = Number(dateStr.slice(0, 4));
        month = Number(dateStr.slice(4, 6)) - 1;
    } else {
        year = today.year();
        month = today.month();
    }
    if (!parsedDate.isValid() && dateStr && month >= 0 && month <= 11) {
        dateStr = moment([year, month]).startOf("month").format('YYYYMMDD');
    } else if (!parsedDate.isValid() && dateStr && (month < 0 || month > 11)) {
        month = 0;
        dateStr = moment([year, 0]).startOf("month").format('YYYYMMDD');
    }
    return {
        year: year,
        month: month,
        dateStr: dateStr
    };
}